#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>

#define NN 64

int main(int argc, char const* argv[])
{
    if(3!=argc)
    {
        printf("Usage: %s <ip> <port>",argv[0]);
        return -1;
    }
    int client_socket = socket(AF_INET,SOCK_DGRAM,0);
    if (client_socket==-1)
    {
        perror("socket err\n");
        return -1;
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(atoi(argv[2]));
    server_addr.sin_addr.s_addr=inet_addr(argv[1]);

    char buf[64]={'\0'};
    while (1)
    {
        memset(buf,0,64);
        fgets(buf,64,stdin);
        ssize_t ret = sendto(client_socket,buf,strlen(buf),0,(struct sockaddr*)&server_addr,sizeof(server_addr));
        if(ret<0)
        {
            perror("sendto err\n");
            return -1;
        }

        ssize_t len=recvfrom(client_socket,buf,64,0,NULL,NULL);
        if (len>0)
        {
            printf("recv data = %s\n",buf);
        }
        
    }
    
        
        
    close(client_socket);

    return 0;
}